package com.hibase.core.page.service.pageuserconfig.impl;


import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.hibase.common.constant.HibaseBaseStatus;
import com.hibase.common.exception.HibaseAssert;
import com.hibase.common.util.UserContext;
import com.hibase.core.mybatis.service.BaseServiceImpl;
import com.hibase.core.page.constant.PageUserConfigType;
import com.hibase.core.page.entity.pageuserconfig.PageUserConfig;
import com.hibase.core.page.entity.pageuserconfig.vo.PageUserConfigSearchVO;
import com.hibase.core.page.mapper.pageuserconfig.PageUserConfigMapper;
import com.hibase.core.page.service.pageuserconfig.PageUserConfigService;
import com.hibase.core.web.entity.OperationUserIdEntity;

import cn.hutool.core.util.StrUtil;

/**
 * <p>
 * 菜单配置内容表 服务实现类
 * </p>
 *
 * @author junk
 * @since 2021-03-31
 */
@Service
public class PageUserConfigServiceImpl extends BaseServiceImpl<PageUserConfigMapper, PageUserConfig> implements PageUserConfigService {

    /**
     * 查询菜单配置内容表列表
     */
    @Override
    public PageInfo<PageUserConfig> selectPageUserConfigList(PageUserConfigSearchVO searchVO) {

        return super.page(searchVO, new QueryWrapper<PageUserConfig>());
    }

    /**
     * 改变菜单配置内容表记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePageUserConfig(PageUserConfig entity) {

        // 根据菜单id 和用户id
        String menuId = entity.getMenuId();
        String type = entity.getType();
        String userId = UserContext.get().getId();
        PageUserConfig config = this.getOne(new QueryWrapper<PageUserConfig>().eq("menu_id", menuId)
                .eq("user_id", userId)
                .eq("type", type));
        if (config != null){
            entity.setId(config.getId());
        }
        if(StrUtil.isBlank(entity.getStatus())) {
        	entity.setStatus(HibaseBaseStatus.ENABLE.getKey());
        }
        entity.setUserId(userId);
        super.store(entity);
    }

    /**
     * 查询菜单配置内容表详情
     */
    @Override
    public PageUserConfig viewPageUserConfig(String id) {

        PageUserConfig entity = super.getById(id, true);

        return entity;
    }

    /**
     * 删除菜单配置内容表记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePageUserConfig(String id) {

        super.deleteBy(id);
    }


    /**
     * 导出菜单配置内容表记录
     */
    @Override
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    public void exportPageUserConfig(PageUserConfigSearchVO searchVO, HttpServletResponse response) {

//        StringBuilder fileName = new StringBuilder();
//        fileName.append("export-");
//        fileName.append("PageUserConfig");
//        fileName.append(DateUtil.today());
//        Sheet sheet = null;
//        sheet = new Sheet(1, 1, PageUserConfigExportVO.class, fileName.toString(), null);
////todo
//        AttachmentUtils.exportExcelFile(response, sheet, null, fileName.toString());
    }

    /**
     * (non-Javadoc)
     * <p>Title: getSelectCondByUserIdAndPageId</p>
     * <p>Description: 查询页面初始化条件，BY 用户 BY pageId</p>
     * @author TimBrian
     * @since 2022年2月16日 上午11:33:24
     * @param searchVo
     * @return
     * @see com.hibase.core.page.service.pageuserconfig.PageUserConfigService#getSelectByUserIdAndPageId(com.hibase.core.web.entity.OperationUserIdEntity)
     */
	@Override
	public PageUserConfig getSelectByUserIdAndPageId(OperationUserIdEntity searchVo) {
		HibaseAssert.isTrue(PageUserConfigType.SELECT.getKey().equals(searchVo.getType()), "类型不匹配");
		PageUserConfig entity = super.getOne(Wrappers.<PageUserConfig>lambdaQuery().eq(PageUserConfig::getUserId, UserContext.get().getId())
				.eq(PageUserConfig::getMenuId, searchVo.getMenuId()).eq(PageUserConfig::getType, PageUserConfigType.SELECT.getKey()));
		return entity;
	}

}
